﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Training.Domain;

namespace Training.Presentation
{
    public class Screen
    {
        public static void StartApplication()
        {
            Console.Clear();
            switch (Menu.StartMenu())
            {
                case StartMenuEnum.ListPersons:
                    Program.ListsMenu();
                    break;
                case StartMenuEnum.CreatePerson:
                    Screen.CreateItem();
                    break;
                default:
                    break;
            }
        }

        public static int ShowList(List<Person> persons)
        {            
            Console.Clear();
            for (int i = 0; i < persons.Count(); i++)
            {
                var person = persons[i];
                Console.WriteLine(string.Format("{0}- {1}, {2} {3}, {4} Years old, {5} Euros",
                    i,
                    person.Vat, 
                    person.Name, 
                    person.Surname, 
                    person.Age().ToString("N2"), 
                    person.Salary));
            }

            Console.WriteLine("Select number row to make actions");
            return ValidateIfIdentifierExits(persons.Count(), Validation.ValidateReadLineInteger());
        }

        private static int ValidateIfIdentifierExits(int numberOfItems, int selection)
        {
            if (selection > numberOfItems || selection < 0)
            {
                Console.WriteLine("Enter an existing item");
                return ValidateIfIdentifierExits(numberOfItems, Validation.ValidateReadLineInteger());
            }
            else
                return selection;
        }

        public static void ShowItem(Person person)
        {
            Console.Clear();

            var _action = Menu.EditItemMenu(person);

            switch (_action)
            {
                case EditItemMenu.Exit:
                    StartApplication();
                    break;
                case EditItemMenu.RemoveItem:
                    Program.RemoveItem(person.Vat);
                    break;
                default:
                    EditField(person, (int)_action);
                    break;
            }
        }

        private static void EditField(Person person, int indexEnumFiled)
        {
            Console.WriteLine(string.Format("Person VAT: {0}", person.Vat));
            var _labelFieldEditting = person.GetLabelFieldToEdit(indexEnumFiled);
            var _fieldValueEditting = person.GetValueFieldToEdit(indexEnumFiled);
            Console.WriteLine(string.Format("Edit {0}, OldValue {1}", _labelFieldEditting, _fieldValueEditting));

            Console.WriteLine();
            Console.WriteLine("Enter the new value. Respect the format");
            var newValue = Console.ReadLine();
            Program.SaveValue(person, indexEnumFiled, newValue);
        }

        public static void CreateItem()
        {
            var _person = new Person();

            Console.Clear();
            Console.WriteLine("Enter Vat Number:");
            _person.Vat = Console.ReadLine();

            Console.WriteLine("Enter Name:");
            _person.Name = Console.ReadLine();

            Console.WriteLine("Enter Surname:");
            _person.Surname = Console.ReadLine();

            Console.WriteLine("Enter Birth Date (dd/mm/yyyy):");
            _person.Birthdate = Validation.ValidateReadLineDateTime();

            Console.WriteLine("Enter Salary (15000.50):");
            _person.Salary = Validation.ValidateReadLineDecimal();

            Program.SaveItem(_person);
        }
    }
}
